using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.OrdersManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using NUglify.Helpers;

namespace Business.OrdersManagement
{
    [Authorize(BusinessPermissions.Orders.Default)]
    public class OrdersAppService : ApplicationService,IOrdersAppService
    {
        private const string FormName = "Orders";
        private IRepository<Orders, Guid> _repository;
        private IRepository<Warehouse, Guid> _warerepository;
        private IRepository<Province, Guid> _shengrepository;
        private IRepository<Shi, Guid> _shirepository;
        private IRepository<Region, Guid> _regionrepository;

        public OrdersAppService(
            IRepository<Orders, Guid> repository, IRepository<Warehouse, Guid> warerepository, IRepository<Province, Guid> shengrepository, IRepository<Shi, Guid> shirepository, IRepository<Region, Guid> regionrepository
            )
        {
            _repository = repository;
            _warerepository=warerepository;
            _shengrepository = shengrepository;
            _shirepository=shirepository;
            _regionrepository=regionrepository;
        }
        #region 增删改查基础方法

        public async Task<OrdersDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Orders, OrdersDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<OrdersDto>> GetAll(GetOrdersInputDto input)
        {
            var warelist = (await _warerepository.GetListAsync());
            var orderlist = (await _repository.GetListAsync()); 
            var shenglist=await _shengrepository.GetListAsync();
            var shilist=await _shirepository.GetListAsync();
            var qulist=await _regionrepository.GetListAsync();
            var query = (from a in warelist
                        join b in orderlist on a.WarehouseNo equals b.WarehouseNo
                        join c in qulist on b.PCode equals  c.RegionNo
                        where(b.OrderStatues==false)
                        select new OrdersDto
                        {
                            IsDelivery = b.IsDelivery,
                            Salesman = b.Salesman,
                            ServerceCompany = b.ServerceCompany,
                            BuyerRemark = b.BuyerRemark,
                            Deliverytime = b.Deliverytime,
                            Id = b.Id,
                            WarehouseNo = b.WarehouseNo,
                            OrderName = b.OrderName,
                            WarehouseName = a.WarehouseName,
                            OrderShop = b.OrderShop,
                            OrderStyle = b.OrderStyle,
                            OrderAddr = b.OrderAddr,
                            OrderDate = b.OrderDate,
                            OrderEmail = b.OrderEmail,
                            OrderFreight = b.OrderFreight,
                            OrderNo = b.OrderNo,
                            OrderPhone = b.OrderPhone,
                            OrderRemark = b.OrderRemark,
                            OrderTelphone = b.OrderTelphone,
                            Ispresell = b.Ispresell,
                            PCode = b.PCode,
                            RegionName=c.RegionName,
                            Recipients = b.Recipients,
                            OrderHtml = b.OrderHtml,
                            OrderStatues = b.OrderStatues,
                        }).AsQueryable();

            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.OrderNo.Contains(input.Filter));

            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.OrderName.Contains(input.Filter));

            var totalCount =  query.Count();
            var items =  query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount)
                       .Take(input.MaxResultCount)
                       .ToList();

            //var dto = ObjectMapper.Map<List<Orders>, List<OrdersDto>>(items);
            return new PagedResultDto<OrdersDto>(totalCount, items);
        }

        public async Task<OrdersDto> DataPost(CreateOrUpdateOrdersDto input)
        {
            Orders result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateOrdersDto, Orders>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Orders, OrdersDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

        public async Task Update(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                var list = (await _repository.GetListAsync()).Where(x => x.Id ==item);
                list.ForEach(x =>
                {
                    x.OrderStatues = true;
                    _repository.UpdateManyAsync(list);
                });
            }
            
        }


        #endregion

    }
}